<template>
  <div>
    <!-- 展示 sku 表格 -->
    <el-table :data="recored" style="width: 100%" border>
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="名称" prop="skuName" />
      <el-table-column label="描述" prop="skuDesc" />
      <el-table-column label="默认图片">
        <template slot-scope="{ row }">
          <img :src="row.skuDefaultImg" style="width: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="重量" prop="weight" />
      <el-table-column label="价格" prop="price" />
      <el-table-column label="操作" width="300">
        <template slot-scope="{ row }">
          <HintButton v-if="row.isSale == 1" title="上架" size="mini" type="success" icon="el-icon-arrow-up" @click="shangjiaOrxiajia(row, '1')" />
          <HintButton v-else title="下架" size="mini" type="success" icon="el-icon-arrow-down" @click="shangjiaOrxiajia(row, '0')" />
          <HintButton title="修改" size="mini" type="primary" icon="el-icon-edit" />
          <HintButton title="查看" size="mini" type="info" icon="el-icon-info" @click="selectSku(row)" />
          <HintButton title="删除" size="mini" type="danger" icon="el-icon-delete" />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination :current-page="page" :page-sizes="[3, 5, 10]" :page-size="limit" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleCurrentChange" @current-change="getSkuList" />
    <!-- 抽屉 -->
    <el-drawer :visible.sync="drawer" :show-close="false" size="50%">
      <!-- row -->
      <el-row>
        <el-col :span="6" class="skutitle"> 名称</el-col>
        <el-col :span="12" class="skuattr">{{ skuInfo.skuName }} </el-col>
      </el-row>
      <!-- row -->
      <el-row>
        <el-col :span="6" class="skutitle"> 描述</el-col>
        <el-col :span="12" class="skuattr">{{ skuInfo.skuDesc }} </el-col>
      </el-row>
      <!-- row -->
      <el-row>
        <el-col :span="6" class="skutitle"> 价格</el-col>
        <el-col :span="12" class="skuattr">{{ skuInfo.price }} </el-col>
      </el-row>
      <!-- row -->
      <el-row>
        <el-col :span="6" class="skutitle"> 平台属性</el-col>
        <el-col :span="12" class="skuattr">
          <el-tag v-for="item in skuInfo.skuAttrValueList" :key="item.id" type="success">{{ item.attrId }}--{{ item.valueId }}</el-tag>
        </el-col>
      </el-row>
      <!-- row -->
      <el-row>
        <el-col :span="6" class="skutitle"> 商品图片</el-col>
        <el-col :span="12" class="skuattr">
          <div class="block">
            <span class="demonstration" />
            <el-carousel height="150px">
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item">
                <h3 class="small">
                  <img :src="item.imgUrl" style="width: 100%; height: 100%" />
                </h3>
              </el-carousel-item>
            </el-carousel>
          </div>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      page: 1,
      limit: 5,
      total: 0,
      recored: [], // sku 信息列表
      skuInfo: {},
      // 抽屉控制变量
      drawer: false,
    }
  },
  mounted() {
    this.getSkuList()
  },
  methods: {
    // 获取 sku 信息列表
    async getSkuList(pages = 1) {
      // 默认为请求第一页,触发分页器函数时,pages的值等于分页器传递过来的值
      this.page = pages
      const { page, limit } = this
      const res = await this.$api.sku.reqSkuList(page, limit)
      console.log(res)
      if (res.code === 200) {
        this.recored = res.data.records
        this.total = res.data.total
      }
    },
    // 分页器的回调
    handleCurrentChange(limit) {
      console.log(limit, 'limit')
      this.limit = limit
      this.getSkuList()
    },
    // 上架或下架 sku
    async shangjiaOrxiajia(row, e) {
      if (e === '1') {
        const res = await this.$api.sku.reqCancelSale(row.id)
        this.getSkuList()
      } else {
        const res = await this.$api.sku.reqOnSale(row.id)
        this.getSkuList()
      }
    },
    // 查看当前 sku 信息
    async selectSku(row) {
      this.drawer = true
      const res = await this.$api.sku.reqGetSkuById(row.id)
      console.log(res)
      if (res.code === 200) {
        this.skuInfo = res.data
      }
    },
  },
}
</script>

<style soped>
.skuattr {
  margin-left: 10px;
  font-size: 16px;
}
.skutitle {
  text-align: right;
  font-size: 20px;
  font-weight: 400;
}
.el-row {
  margin-bottom: 15px;
}
.el-tag {
  margin: 5px;
}
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
